import { handleMessage } from '@/utils/globalEvent';
import type { IFormatAxis } from './type';

/**
 * @description: 时间问候语
 */
const formatAxis: IFormatAxis = () => {
	// 获取当前时间
	const hour: number = new Date().getHours();
	const welcome = ',欢迎回来!';
	// 判断数组
	const hourForNameMap = [
		[(hour: number) => hour < 6, () => '凌晨好'],
		[(hour: number) => hour < 9, () => '早上好'],
		[(hour: number) => hour < 12, () => '上午好'],
		[(hour: number) => hour < 14, () => '中午好'],
		[(hour: number) => hour < 17, () => '下午好'],
		[(hour: number) => hour < 19, () => '傍晚好'],
		[(hour: number) => hour < 22, () => '晚上好'],
	];
	// 获取符合条件的子数组
	const getGreetings = hourForNameMap.find(item => item[0](hour));
	// 子数组存在则运行子数组中的第二个元素（执行函数）
	getGreetings?.length ? handleMessage(`${getGreetings[1](hour)}${welcome}`) : handleMessage(`夜里好${welcome}`);
};

export { formatAxis };
